package src.automouser;

import java.awt.*;
import java.awt.event.*;
import java.net.URL;
//import java.util.logging.Level;
//import java.util.logging.Logger;
//import java.util.logging.Level;
//import java.util.logging.Logger;
import javax.swing.*;
//import java.awt.Robot;

public class autoMouser {
    public static void main(String[] args) {
        /* Use an appropriate Look and Feel */
        try {
            UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
            //UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
        } catch (UnsupportedLookAndFeelException ex) {
            ex.printStackTrace();
        } catch (IllegalAccessException ex) {
            ex.printStackTrace();
        } catch (InstantiationException ex) {
            ex.printStackTrace();
        } catch (ClassNotFoundException ex) {
            ex.printStackTrace();
        }
        /* Turn off metal's use of bold fonts */
        UIManager.put("swing.boldMetal", Boolean.FALSE);
        //Schedule a job for the event-dispatching thread:
        //adding TrayIcon.
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                try {
                    createAndShowGUI();
                } catch (AWTException ex) {
                    System.out.println("Something got fucked up...");
                }
            }
        });
    }

    private static void createAndShowGUI() throws AWTException {
        //Check the SystemTray support
        if (!SystemTray.isSupported()) {
            System.out.println("SystemTray is not supported");
            return;
        }
        final PopupMenu popup = new PopupMenu();
        final TrayIcon trayIcon =
                new TrayIcon(createImage("images/bulb.gif", "tray icon"));
        final SystemTray tray = SystemTray.getSystemTray();

        // Create a popup menu components
        MenuItem aboutItem = new MenuItem("About");
        MenuItem exitItem = new MenuItem("Exit");
        MenuItem startClickingItem = new MenuItem("Start clicking");

        //Add components to popup menu
        popup.add(startClickingItem);
        popup.add(aboutItem);
        popup.addSeparator();
        popup.add(exitItem);

        trayIcon.setPopupMenu(popup);
        trayIcon.setToolTip("autoMouser 0.1.0");

        try {
            tray.add(trayIcon);
        } catch (AWTException e) {
            System.out.println("TrayIcon could not be added.");
            return;
        }

        //final Robot myScreen = new Robot();
        final autoMouserRobot autoMR = new autoMouserRobot();

        trayIcon.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                JOptionPane.showMessageDialog(null,
                        "autoMouser 0.1.0");
            }
        });

        aboutItem.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                JOptionPane.showMessageDialog(null,
                        "This is autoMouser - simple software for flash game autoplaying.\n" +
                        "Press F12 to start clicking... (not implemented yet)");
            }
        });

        startClickingItem.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                while(Toolkit.getDefaultToolkit().getLockingKeyState(KeyEvent.VK_CAPS_LOCK)){
                    autoMR.mouseRobot(460, 538, 3);
                    autoMR.mouseRobot(396, 574, 3);
                    autoMR.mouseRobot(532, 573, 3);
                    autoMR.mouseRobot(472, 613, 3);
                    autoMR.mouseRobot(607, 610, 3);
                    autoMR.mouseRobot(545, 649, 3);
                    autoMR.mouseRobot(699, 437, 3);
                    autoMR.mouseRobot(650, 463, 3);
                    autoMR.mouseRobot(593, 492, 3);
                    autoMR.wait(1000*30); //wait 30 sec
                    /*
                     * Last line hangs this software. Should also recognize Caps-Lock state change.
                     */

                }
                
            }
        });

        exitItem.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                tray.remove(trayIcon);
                System.exit(0);
            }
        });
    }

    //Obtain the image URL
    protected static Image createImage(String path, String description) {
        URL imageURL = autoMouser.class.getResource(path);

        if (imageURL == null) {
            System.err.println("Resource not found: " + path);
            return null;
        } else {
            return (new ImageIcon(imageURL, description)).getImage();
        }
    }
}
